package com.fs.mjx.controller;

import com.fs.mjx.pojo.User;
import com.fs.mjx.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @description:
 * @author： mjx
 * @createDate: 2020/9/23
 * @version: 1.0
 */
//该注解表明这个类是一个由IOC控制的bean
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired//依赖注入 自动装配userService
    private UserService userService;



    @GetMapping("/getview")
    public String getListView(){
        return "userList";
    }

    @RequestMapping("/getlist")
    @ResponseBody//告诉框架返回的格式为json字符串类型，放在响应体里面
    public Map<String,Object> getList(){

        List<User> userSet = userService.getAll();
        System.out.println(userSet);

        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取用户数据成功");
        result.put("userList",userSet);
        return result;
    }

    @RequestMapping("/getListByPage")
    @ResponseBody//告诉框架返回的格式为json字符串类型，放在响应体里面
    public Map<String,Object> getListByPage(Integer currentPage,
                                            Integer pageSize){

        PageInfo<User> pageInfo = userService.getUsersByPage(currentPage, pageSize);
//        System.out.println(pageInfo.getList());

        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取用户数据成功");
        result.put("userList",pageInfo.getList());//返回查询出来的分页数据
        result.put("totalPage",pageInfo.getPages());//返回当前总共有多少页
        return result;
    }

    @RequestMapping("/getListByPageForQuery")
    @ResponseBody//告诉框架返回的格式为json字符串类型，放在响应体里面
    public Map<String,Object> getListByPageForQuery(Integer currentPage,
                                            Integer pageSize,String address){

        PageInfo<User> pageInfo = userService.getUsersByPageForQuery(currentPage, pageSize,address);
//        System.out.println(pageInfo.getList());

        Map<String,Object> result = new HashMap<>();

        result.put("code",0);
        result.put("message","获取用户数据成功");
        result.put("userList",pageInfo.getList());//返回查询出来的分页数据
        result.put("totalPage",pageInfo.getPages());//返回当前总共有多少页
        return result;
    }

    @RequestMapping("/getOne")
    @ResponseBody
    public Map<String,Object> getOne(Integer userId){
        Map<String,Object> result = new HashMap<>();
        User user = userService.getOneByUserId(userId);
        if(user == null){
            result.put("code",-1);
            result.put("message","获取用户数据失败");

        }else{
            result.put("code",0);
            result.put("message","获取用户数据成功");
            result.put("user",user);
        }
        return result;
    }

    @RequestMapping("/edit")
    @ResponseBody
    public Map<String,Object> edit(User user){
        Map<String,Object> result = new HashMap<>();
        boolean modified;
        if(user.getUserId()>0) {
             modified = userService.modifyOne(user);
        }else {
            modified = userService.addOne(user);
        }
        if(!modified){
            result.put("code",-1);
            result.put("message","编辑用户数据失败");

        }else{
            result.put("code",0);
            result.put("message","编辑用户数据成功");
            result.put("user",user);
        }
        return result;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(User user){
        Map<String,Object> result = new HashMap<>();
         boolean deleted = userService.removeOne(user);
        if(!deleted){
            result.put("code",-1);
            result.put("message","删除失败");
        }else{
            result.put("code",0);
            result.put("message","删除成功");
        }
        return result;
    }

    @RequestMapping("/queryUserByAddress")
    @ResponseBody
    public Map<String,Object> queryUserByAddress(String address){
//        System.out.println(text);
        Map<String,Object> result = new HashMap<>();
        List<User> userSet = userService.getUserThatAddressLike(address);
        result.put("code",0);
        result.put("message","获取用户数据成功");
        result.put("userList",userSet);
        result.put("address",address);
        return result;
    }
}
